package com.gs.djys.Opinion.opinionmx;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gs.djys.R;
import com.gs.djys.Util.ToastUtils;
import com.gs.djys.checkfragments.license.LicenseFragment;
import com.gs.djys.checkfragments.opinion.OpinionFragment;
import com.gs.djys.mvp.MVPBaseActivity;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class OpinionMXActivity extends MVPBaseActivity<OpinionMXContract.View, OpinionMXPresenter> implements OpinionMXContract.View {

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title_text)
    TextView titleText;
    @Bind(R.id.cancle_button)
    TextView cancleButton;
    @Bind(R.id.xkxx_te)
    TextView xkxxTe;
    @Bind(R.id.splc_te)
    TextView splcTe;
    @Bind(R.id.tab_lay)
    LinearLayout tabLay;
    @Bind(R.id.fragment_container)
    FrameLayout fragmentContainer;
    private LicenseFragment licenseFragment;
    private OpinionFragment opinionFragment;

    @Override
    public void onRequestError(String msg) {
        ToastUtils.showShortToast(msg);
    }

    @Override
    protected int getLayout() {
        return R.layout.act_opinionmx;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        goBack();
        setTitleText("征求意见");
        licenseFragment = new LicenseFragment();
        opinionFragment = new OpinionFragment();
        goToFragment(licenseFragment);
    }
    @OnClick({R.id.xkxx_te, R.id.splc_te})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.xkxx_te:
                goToFragment(licenseFragment);
                xkxxTe.setBackgroundColor(getResources().getColor(R.color.tab_bg));
                splcTe.setBackgroundColor(getResources().getColor(R.color.white));
                break;
            case R.id.splc_te:
                goToFragment(opinionFragment);
                xkxxTe.setBackgroundColor(getResources().getColor(R.color.white));
                splcTe.setBackgroundColor(getResources().getColor(R.color.tab_bg));
                break;
        }
    }

    private Fragment mContent = null;

    /**
     * 修改显示的内容 不会重新加载
     **/
    public void goToFragment(Fragment to) {
        if (mContent != to) {
            FragmentTransaction transaction = getSupportFragmentManager()
                    .beginTransaction();
            if (!to.isAdded()) { // 先判断是否被add过
                if (mContent != null)
                    transaction.hide(mContent).add(R.id.fragment_container, to).commitAllowingStateLoss(); // 隐藏当前的fragment，add下一个到Activity中
                else
                    transaction.add(R.id.fragment_container, to).commitAllowingStateLoss();
            } else {
                if (mContent != null)
                    transaction.hide(mContent).show(to).commitAllowingStateLoss(); // 隐藏当前的fragment，显示下一个
                else
                    transaction.show(to).commitAllowingStateLoss();
            }
            mContent = to;
        }
    }
}
